package com.ktjy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktjy.mapper.SysUserMapper;
import com.ktjy.pojo.SysUser;
import com.ktjy.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jjx
 * @since 2025-07-23
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {
    @Autowired
    RedisTemplate redisTemplate;

    @Override
    @Cacheable(value = "user", key="#id")
    public SysUser getUser(Long id) {
//        //1.直接查询redis
//        SysUser sysUser = (SysUser) redisTemplate.opsForValue().get("user::" + id);
//        //1.1.如果redis没有，就查询数据库，并把数据保存到redis
//        if(sysUser==null){
//            sysUser = this.getById(id);
//
//            //数据保存到redis
//            redisTemplate.opsForValue().set("user::" + id,sysUser);
//        }
//        //1.2.如果redis有，直接返回
//        return sysUser;
//
        return this.getById(id);
    }

    @Override
    //直接删除缓存
//    @CacheEvict(value = "user", key="#sysUser.getUsrId()")
    @CachePut(value = "user", key="#sysUser.getUsrId()")
    public boolean saveOrUpdateUser(SysUser sysUser) {
        boolean ok= this.saveOrUpdate(sysUser);
//        if(ok){
//            redisTemplate.delete("user::"+sysUser.getUsrId());
//        }
        return ok;
    }
}
